package com.edu.framework.websocket.handler;

import cn.hutool.core.util.StrUtil;
import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.highlights.HighlightsRequest;
import com.edu.framework.websocket.message.notice.ForceNoticeRequest;
import com.edu.framework.websocket.message.notice.NoticeRequest;
import com.edu.framework.websocket.service.ForceNoticeService;
import com.edu.framework.websocket.service.NoticeService;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class ForceNoticeRequestHandler implements IHandler<ForceNoticeRequest> {

    @Resource
    private ForceNoticeService wsForceNoticeService;


    @Override
    public void execute(WebSocketSession session, ForceNoticeRequest message) {
        if (message == null) {
            return;
        }

        Map<String, String> data = message.getData();
        String classId = data.get(ForceNoticeRequest.CLASS_ID_KEY);

        Response response = null;
        if (StrUtil.isBlank(classId)){
            response = Response.fail(HighlightsRequest.TYPE, "id不能为空");
        } else {
            response = wsForceNoticeService.getForceNoticeList(Long.parseLong(classId));
        }
        try {
            WebSocketHolder.send(session, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public String getType() {
        return ForceNoticeRequest.TYPE;
    }
}
